const app = getApp()

Page({
  data: {
    faceUrl: "../resource/images/noneface.png",
    isMe: true,
    isFollow: false,
    fansCounts: 0,
    followCounts: 0,
    userId: "",

    videoSelClass: "video-info",
    isSelectedWork: "video-info-selected",
    isSelectedLike: "",
    isSelectedFollow: "",

    myWorkFlag: false,
    myLikeFlag: true,
    myFollowFlag: true,

    myVideoList: [],
    myVideoPage: 1,
    myVideoTotal: 1,

    likeVideoList: [],
    likeVideoPage: 1,
    likeVideoTotal: 1,

    followVideoList: [],
    followVideoPage: 1,
    followVideoTotal: 1,

    
  },
  //注销方法
  logout: function (e) {
    var user = app.getGlobalUserInfo();
    var url = app.serverUrl;
    //调用请求接口
    wx.request({
      url: url + '/logout?userId=' + user.id,
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      //接口调用成功
      success: function (res) {
        wx.hideLoading();
        var status = res.data.status;
        if (status == 200) {
          wx.showToast({
              title: '注销成功~',
              icon: 'success',
            }),
            //app.userInfo = null,
            wx.removeStorageSync("userInfo");
          wx.navigateTo({
            url: '../userLogin/login'
          })
        } else if (status == 500) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 3000
          })

        }
      }
    })
  },
  //载入页面时请求
  onLoad: function (params) {
    var me = this;
    var user = app.getGlobalUserInfo();
    var userId = user.id;
    var url = app.serverUrl;
    var publisherId = params.publisherId;
    if (publisherId != null && publisherId != undefined && publisherId != '') {
      userId = publisherId;
      //不是自己
      if (user.id != publisherId) {
        me.setData({
          isMe: false
        })
      }
    }
    //设置userId为发布者或自己,存储发布者id
    me.setData({
      userId: userId,
      publisherId: publisherId
    })
  
    //提示用户请求已发送
    wx.showLoading({
      title: '请等待...'
    })
    url = app.serverUrl;
    //调用请求接口
    wx.request({
      url: url + '/user/query?userId=' + userId + '&fanId=' + user.id,
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'userId': user.id,
        'userToken': user.userToken
      },
      //接口调用成功
      success: function (res) {
        wx.hideLoading();
        var status = res.data.status;
        if (status == 200) {
          // wx.showToast({
          //   title: '登录成功~',
          //   icon: 'success',
          // })
          var userInfo = res.data.data;
          var faceUrl = "../resource/images/noneface.png";
          if (userInfo.faceImage != null && userInfo.faceImage != '' && userInfo.faceImage != undefined) {
            faceUrl = url + userInfo.faceImage;
          }
          //debugger;
          me.setData({
            faceUrl: faceUrl,
            fansCounts: userInfo.fansCounts,
            followCounts: userInfo.followCounts,
            receiveLikeCounts: userInfo.receiveLikeCounts,
            nickname: userInfo.nickname,
            isFollow: userInfo.follow
          });
          me.getMyVideoList(1);
        } else if (status == 502) {
          wx.showToast({
            title: res.data.msg,
          })
          //跳转到登录页
          wx.redirectTo({
            url: '../userLogin/login',
          })
        }
      }
    })
  },
  //更改头像
  changeFace: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        var serverUrl = app.serverUrl;
        wx.showLoading({
          title: '上传中...',
        });
        var userInfo = app.getGlobalUserInfo();
        wx.uploadFile({
          url: serverUrl + '/user/uploadFace?userId=' + userInfo.id,
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'content-type': 'application/json',
            'userId': userInfo.id,
            'userToken': userInfo.userToken
          },
          success(res) {
            const data = JSON.parse(res.data);

            //do something
            var status = data.status;
            wx.hideLoading({})
            if (status == 200) {
              wx.showToast({
                title: '上传成功!',
                icon: 'success'
              });
              //获取头像相对路径
              var imageUrl = data.data;
              that.setData({
                faceUrl: serverUrl + imageUrl
              });
            } else if (status == 500) {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 3000
              })
            } else if (status == 502) {
              wx.showToast({
                title: res.data.msg,
              })
              //跳转到登录页
              wx.navigateTo({
                url: '../userLogin/login',
              })
            }

          }
        })
      }
    })
  },
  uploadVideo: function () {
    var me = this;
    wx.chooseVideo({
      sourceType: ['album'],
      success(res) {
        var duration = res.duration;
        var height = res.height;
        var width = res.width;
        var size = res.size;
        var tempVideoUrl = res.tempFilePath;
        var tempCoverUrl = res.thumbTempFilePath;
        if (duration > 16) {
          wx.showToast({
            title: '视频时长不能超过15秒哦...',
            icon: 'none',
            duration: 2500
          })
        } else if (duration < 2) {
          wx.showToast({
            title: '视频时长不能少于2秒哦...',
            icon: 'none',
            duration: 2500
          })
        } else {
          //TODO 跳转到选择bgm的页面
          wx.navigateTo({
            url: '../chooseBgm/chooseBgm?duration=' + duration +
              "&height=" + height +
              "&width=" + width +
              "&size=" + size +
              "&tempVideoUrl=" + tempVideoUrl +
              "&tempCoverUrl=" + tempCoverUrl,
          })

        }
      }
    })
  },
  //关注与取消关注
  focusMe: function (e) {
    var me = this;
    var user = app.getGlobalUserInfo();
    var userId = user.id;
    var publisherId = me.data.publisherId;

    var followType = e.currentTarget.dataset.followtype;
    var url = '';
    var serverUrl = app.serverUrl;
    //debugger;
    //判断是关注还是取消关注
    if (followType == '1') {
      url = '/user/userFollow?loginUserId=' + userId + '&beFocusUserId=' + publisherId;
    } else {
      url = '/user/userUnFollow?loginUserId=' + userId + '&beFocusUserId=' + publisherId;
    }
    wx.showLoading({
      title: '...',
    })
    wx.request({
      url: serverUrl + url,
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'userId': user.id,
        'userToken': user.userToken
      },
      success: function (res) {
        wx.hideLoading();
        if (followType == '1') {
          me.setData({
            isFollow: true,
            fansCounts: me.data.fansCounts + 1
          })
        } else {
          me.setData({
            isFollow: false,
            fansCounts: me.data.fansCounts - 1
          })
        }
      }

    })
  },

  //点击tab栏-作品
  doSelectWork: function () {
    var me = this;
    me.setData({
      isSelectedWork: "video-info-selected",
      isSelectedLike: "",
      isSelectedFollow: "",

      myWorkFlag: false,
      myLikeFlag: true,
      myFollowFlag: true,

      myVideoList: [],
      myVideoPage: 1,
      myVideoTotal: 1,

      likeVideoList: [],
      likeVideoPage: 1,
      likeVideoTotal: 1,

      followVideoList: [],
      followVideoPage: 1,
      followVideoTotal: 1,

      
    });
    //调用查询方法
    me.getMyVideoList(1);

  },
  //点击tab栏-喜欢
  doSelectLike: function () {
    var me = this;
    me.setData({
      isSelectedWork: "",
      isSelectedLike: "video-info-selected",
      isSelectedFollow: "",

      myWorkFlag: true,
      myLikeFlag: false,
      myFollowFlag: true,

      myVideoList: [],
      myVideoPage: 1,
      myVideoTotal: 1,

      likeVideoList: [],
      likeVideoPage: 1,
      likeVideoTotal: 1,

      followVideoList: [],
      followVideoPage: 1,
      followVideoTotal: 1,   
    });
    //查询喜欢的视频
    me.getLikeVideoList(1);
  },
  //点击tab栏-关注
  doSelectFollow: function () {
    var me = this;
    me.setData({
      isSelectedWork: "",
      isSelectedLike: "",
      isSelectedFollow: "video-info-selected",

      myWorkFlag: true,
      myLikeFlag: true,
      myFollowFlag: false,

      myVideoList: [],
      myVideoPage: 1,
      myVideoTotal: 1,

      likeVideoList: [],
      likeVideoPage: 1,
      likeVideoTotal: 1,

      followVideoList: [],
      followVideoPage: 1,
      followVideoTotal: 1,

      
    });
    me.getFollowVideoList(1);
  },

  //查询用户上传的视频
  getMyVideoList: function (page) {
    var me = this;

    // 查询视频信息
    wx.showLoading();
    // 调用后端
    var serverUrl = app.serverUrl;
    //debugger;
    wx.request({
      url: serverUrl + '/video/showAll/?page=' + page + '&pageSize=6',
      method: "POST",
      data: {
        userId: me.data.userId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data);
        var myVideoList = res.data.data.rows;
        wx.hideLoading();
        var newVideoList = me.data.myVideoList;
        me.setData({
          myVideoPage: page,
          myVideoList: newVideoList.concat(myVideoList),
          myVideoTotal: res.data.data.total,
          serverUrl: app.serverUrl
        });
      }
    })
  },
  //查询用户喜欢的视频
  getLikeVideoList: function(page){
    var me = this;

    // 查询视频信息
    wx.showLoading();
    // 调用后端
    var serverUrl = app.serverUrl;
    
    wx.request({
      url: serverUrl + '/video/showLikeVideo?page=' + page + '&pageSize=6' + '&userId=' + me.data.userId,
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log("喜欢"+res.data);
        var likeVideoList = res.data.data.rows;
        wx.hideLoading();
        var newVideoList = me.data.likeVideoList;
        me.setData({
          likeVideoPage: page,
          likeVideoList: newVideoList.concat(likeVideoList),
          likeVideoTotal: res.data.data.total,
          serverUrl: app.serverUrl
        });
      }
    })
  },

  //查询用户关注者的视频
  getFollowVideoList: function(page){
    var me = this;

    // 查询视频信息
    wx.showLoading();
    // 调用后端
    var serverUrl = app.serverUrl;
    
    wx.request({
      url: serverUrl + '/video/showFollowVideo?page=' + page + '&pageSize=6' + '&userId=' + me.data.userId,
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log("关注");
        console.log(res.data);
        var followVideoList = res.data.data.rows;
        wx.hideLoading();
        var newVideoList = me.data.followVideoList;
        me.setData({
          followVideoPage: page,
          followVideoList: newVideoList.concat(followVideoList),
          followVideoTotal: res.data.data.total,
          serverUrl: app.serverUrl
        });
      }
    })
  },
  // 点击跳转到视频详情页面
  showVideo: function (e) {
    var me = this;
    console.log(e);

    var myWorkFlag = me.data.myWorkFlag;
    var myLikeFlag = me.data.myLikeFlag;
    var myFollowFlag = me.data.myFollowFlag;
    if (!myWorkFlag) {
      var videoList = me.data.myVideoList;
    } else if (!myLikeFlag) {
      var videoList = me.data.likeVideoList;
    } else if (!myFollowFlag) {
      var videoList = me.data.followVideoList;
    }

    var arrindex = e.target.dataset.arrindex;
    var videoInfo = JSON.stringify(videoList[arrindex]);
    wx.redirectTo({
      url: '../videoInfo/videoInfo?videoInfo=' + videoInfo
    })

  },

  // 下拉加载
  onReachBottom: function () {
    var myWorkFlag = this.data.myWorkFlag;
    var myLikeFlag = this.data.myLikeFlag;
    var myFollowFlag = this.data.myFollowFlag;
    // debugger;
    if (!myWorkFlag) {
      var currentPage = this.data.myVideoPage;
      var totalPage = this.data.myVideoTotal;
      // 获取总页数进行判断，如果当前页数和总页数相等，则不分页
      if (currentPage === totalPage) {
        wx.showToast({
          title: '已经没有视频啦...',
          icon: "none"
        });
        return;
      }
      var page = currentPage + 1;
      this.getMyVideoList(page);
    } else if (!myLikeFlag) {
      var currentPage = this.data.likeVideoPage;
      var totalPage = this.data.myLikesTotal;
      // 获取总页数进行判断，如果当前页数和总页数相等，则不分页
      if (currentPage === totalPage) {
        wx.showToast({
          title: '已经没有视频啦...',
          icon: "none"
        });
        return;
      }
      var page = currentPage + 1;
      this.getMyLikesList(page);
    } else if (!myFollowFlag) {
      var currentPage = this.data.followVideoPage;
      var totalPage = this.data.followVideoTotal;
      // 获取总页数进行判断，如果当前页数和总页数相等，则不分页
      if (currentPage === totalPage) {
        wx.showToast({
          title: '已经没有视频啦...',
          icon: "none"
        });
        return;
      }
      var page = currentPage + 1;
      this.getMyFollowList(page);
    }

  }
})